package com.boyuan.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.boyuan.system.common.BaseResponse;
import com.boyuan.system.common.PageRequest;
import com.boyuan.system.common.R;
import com.boyuan.system.entity.DeviceCamera;
import com.boyuan.system.enums.ErrorCode;
import com.boyuan.system.exception.BusinessException;
import com.boyuan.system.service.IDeviceCameraService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 摄像机表 前端控制器
 * </p>
 *
 * @author ply
 * @since 2023-11-17
 */
@RestController
@RequestMapping("/camera")
public class DeviceCameraController {

    @Resource
    private IDeviceCameraService deviceCameraService;

    @GetMapping("list")
    public BaseResponse<List<DeviceCamera>> list(PageRequest pageRequest) {
        Page<DeviceCamera> page = new Page<>(pageRequest.getPage(),pageRequest.getLimit());
        Page<DeviceCamera> cameraPage = deviceCameraService.page(page);
        List<DeviceCamera> list = cameraPage.getRecords();
        int total = (int) cameraPage.getTotal();
        return R.success(list,total);
    }

    @GetMapping("/listAll")
    public BaseResponse<List<DeviceCamera>> listAll() {
        QueryWrapper<DeviceCamera> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("id","camera_name");
        List<DeviceCamera> list = deviceCameraService.list();
        return R.success(list);
    }

    @GetMapping("/getById")
    public BaseResponse<DeviceCamera> get(Long id) {
        DeviceCamera deviceCamera = deviceCameraService.getById(id);
        return R.success(deviceCamera);
    }

    @PostMapping("/add")
    public BaseResponse<Boolean> add(DeviceCamera deviceCamera) {
        boolean result = deviceCameraService.save(deviceCamera);
        return R.success(result);
    }

    @PostMapping("/edit")
    public BaseResponse<Boolean> edit(DeviceCamera deviceCamera) {
        deviceCamera.setUpdateTime(LocalDateTime.now());
        boolean result = deviceCameraService.updateById(deviceCamera);
        return R.success(result);
    }

    @PostMapping("/remove")
    @Transactional(rollbackFor = Exception.class)
    public BaseResponse<Boolean> remove(Long id) {
        if (id == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        DeviceCamera camera = deviceCameraService.getById(id);
        camera.setUpdateTime(LocalDateTime.now());
        camera.setDeleteTime(LocalDateTime.now());
        deviceCameraService.updateById(camera);
        boolean result = deviceCameraService.removeById(id);
        return R.success(result);
    }

}
